<template>
  <el-drawer
    :title="title"
    v-model="visible"
    :size="width"
    :direction="direction"
    destroy-on-close
    @closed="$emit('drawerClose')">
    <template #title>
      <base-text :content="title" class="el-drawer__title"/>
    </template>
    <div class="base-drawer-loading" v-show="isLoading" v-loading="isLoading"/>
    <slot></slot>
  </el-drawer>
</template>

<script setup>
  import {ref, watch, toRef} from 'vue';
  defineEmits(['drawerClose']);
  const props = defineProps({
    width: {
      type: [String, Number],
      // document.body.clientWidth <= 1440 ? 700 : '50%'
      default: 700
    },
    direction: {
      type: String,
      default: 'rtl'
    },
    title: {
      type: String,
      default: ''
    },
    drawerId: {
      type: Number,
      default: 0
    }
  });
  let drawerId = toRef(props, 'drawerId');
  let [visible, isLoading] = [ref(false), ref(false)];

  const loadingOpen = () => (isLoading.value = true);
  const loadingClose = () => (isLoading.value = false);
  watch(drawerId, () => (visible.value = !!drawerId.value));
  defineExpose({loadingOpen, loadingClose});
</script>

<style scoped lang="less">
  .base-drawer-loading {
    position: absolute !important;
    top: 55px;
    left: 0;
    right: 0;
    bottom: 0;
  }
</style>
